package com.example.opengl.base
import android.opengl.GLES20.*

/**
 * @author: Mr.You
 * @create: 2023-07-18 17:12
 * @description:
 **/
class ShaderHelper {
    companion object{

        fun compileVertexShader(shaderCode:String):Int = compileShader(GL_VERTEX_SHADER,shaderCode)
        fun compileFragmentShader(shaderCode: String):Int = compileShader(GL_FRAGMENT_SHADER,shaderCode)


        fun linkProgram(vertexShaderId:Int,fragmentShaderId:Int):Int{
           val program =  glCreateProgram()
            if (program == 0) return 0
            glAttachShader(program,vertexShaderId)
            glAttachShader(program,fragmentShaderId)
            glLinkProgram(program)
            return program
        }


        /**
         * 加载并编译着色器代码
         * type:着色器类型
         * shaderCode:着色器代码
         * 返回着色器id
         */
        private fun compileShader( type:Int,shaderCode: String): Int {
            val shaderObjectId =   glCreateShader(type)
            if (shaderObjectId == 0){
                return 0
            }
            glShaderSource(shaderObjectId,shaderCode)
            glCompileShader(shaderObjectId)
            return  shaderObjectId
        }
    }
}